package com.jus.player.adapter;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.collection.SparseArrayCompat;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentStatePagerAdapter;

import com.jus.player.fragment.home.SubFollowFragment;
import com.jus.player.fragment.home.SubRecommendFragment;

import java.util.List;


/**
 * Home主页适配器
 * Created by jus on 2025/02/12.
 */
public class HomeAdapter extends FragmentStatePagerAdapter {

    private final List<String> mTitles;
    private final SparseArrayCompat<Fragment> mFragments = new SparseArrayCompat<>();

    public HomeAdapter(FragmentManager fm, List<String> titles) {
        super(fm, BEHAVIOR_RESUME_ONLY_CURRENT_FRAGMENT);
        mTitles = titles;
    }

    @Override
    @NonNull
    public Fragment getItem(int position) {
        Fragment fragment = mFragments.get(position);
        if (fragment == null) {
            switch (position) {
                case 0:
                    fragment = SubFollowFragment.newInstance();
                    break;
                case 1:
                    fragment = SubRecommendFragment.newInstance(1L);
                    break;
                default:
                    fragment = SubRecommendFragment.newInstance(2L);
            }
            mFragments.put(position, fragment);
        }
        return fragment;
    }

    @Override
    public int getCount() {
        return mTitles.size();
    }

    @Nullable
    @Override
    public CharSequence getPageTitle(int position) {
        return mTitles.get(position);
    }
}
